package top.ewind.interceptor;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.jfinal.kit.StrKit;
import io.moquette.interception.InterceptHandler;
import io.moquette.interception.messages.InterceptAcknowledgedMessage;
import io.moquette.interception.messages.InterceptConnectMessage;
import io.moquette.interception.messages.InterceptConnectionLostMessage;
import io.moquette.interception.messages.InterceptDisconnectMessage;
import io.moquette.interception.messages.InterceptPublishMessage;
import io.moquette.interception.messages.InterceptSubscribeMessage;
import io.moquette.interception.messages.InterceptUnsubscribeMessage;
import top.ewind.interceptor.mqtt.DeviceStrategy;
import top.ewind.interceptor.mqtt.IMsgStrategy;
import top.ewind.interceptor.mqtt.UserStrategy;
public class MsgInterceptHandler implements InterceptHandler {

	private static Logger LOG = LoggerFactory.getLogger(MsgInterceptHandler.class.getName());

	//设备认证
	//Client-ID：device.pid+”/”+device.id
	//用户认证
	//Client-ID：字符”user/”+device_user.id
	//管理员验证
	//Client-ID：字符”admin/”+sys_admin.id
	@Override
	public void onConnect(InterceptConnectMessage msg) {
		if(StrKit.notBlank(msg.getClientID())){
			String[] cidArr = msg.getClientID().split("/");
			if(cidArr.length!=2){
				return;
			}
			if(cidArr[0].equals("user")){
				IMsgStrategy ums = new UserStrategy();
				ums.onConnect(cidArr[1]);
			}
			else if(cidArr[0].equals("admin")){
				// do nothing
			}
			else{
				IMsgStrategy dms = new DeviceStrategy();
				dms.onConnect(cidArr[1]);
			}
		}
	}

	@Override
	public void onDisconnect(InterceptDisconnectMessage msg) {
		if(StrKit.notBlank(msg.getClientID())){
			String[] cidArr = msg.getClientID().split("/");
			if(cidArr.length!=2){
				return;
			}
			if(cidArr[0].equals("user")){
				IMsgStrategy ums = new UserStrategy();
				ums.onDisconnect(cidArr[1]);
			}
			else if(cidArr[0].equals("admin")){
				// do nothing
			}
			else{
				IMsgStrategy dms = new DeviceStrategy();
				dms.onDisconnect(cidArr[1]);
			}
		}
	}
	

	@Override
	public void onPublish(InterceptPublishMessage msg) {
		if(StrKit.notBlank(msg.getClientID())){
			String[] cidArr = msg.getClientID().split("/");
			System.out.println(msg.getTopicName());
			if(cidArr.length!=2){
				return;
			}
			if(cidArr[0].equals("user")){
				IMsgStrategy ums = new UserStrategy();
				ums.onPublish(cidArr[0],cidArr[1], msg.getPayload());
			}
			else if(cidArr[0].equals("admin")){
				//do nothing
			}
			else{
				LOG.error("设备消息内容："+msg.getClientID()+"---Topic:"+msg.getTopicName()+"---QOS:"+msg.getQos());
				IMsgStrategy dms = new DeviceStrategy();
				dms.onPublish(cidArr[0],cidArr[1], msg.getPayload());
			}
		}
	
	}


	@Override
	public void onSubscribe(InterceptSubscribeMessage msg) {
		
	}

	@Override
	public void onUnsubscribe(InterceptUnsubscribeMessage msg) {
		
	}

	@Override
	public void onMessageAcknowledged(InterceptAcknowledgedMessage msg) {
		
	}

	/* (non-Javadoc)
	 * @see io.moquette.interception.InterceptHandler#onConnectionLost(io.moquette.interception.messages.InterceptConnectionLostMessage)
	 */
	@Override
	public void onConnectionLost(InterceptConnectionLostMessage msg) {
		if(StrKit.notBlank(msg.getClientID())){
			String[] cidArr = msg.getClientID().split("/");
			if(cidArr.length!=2){
				return;
			}
			if(cidArr[0].equals("user")){
				IMsgStrategy ums = new UserStrategy();
				ums.onDisconnect(cidArr[1]);
			}
			else if(cidArr[0].equals("admin")){
				// do nothing
			}
			else{
				IMsgStrategy dms = new DeviceStrategy();
				dms.onDisconnect(cidArr[1]);
			}
		}
	}

}